var tree, setting = {
    view: {
        nameIsHTML: true
    },
    data: {
        key: {
            title: "title"
        }
    },
    async: {
        enable: true,
        url: "../deviceList.php",
        dataFilter: filter
    },
    callback: {
        onClick: onClick,
        onNodeCreated: onNodeCreated,
        onAsyncSuccess: function () {
            locateSubUser(tree.locateQueue);
        }
    }
};

/*
        if (typeof parent.syncTree == 'function') {
            parent.syncTree();
		}
    */

function getNodeName(nodeText) {
    try {
        //var name = nodeText.substring(0, nodeText.indexOf("["));
        //var count = nodeText.substring(nodeText.indexOf("[") + 1, nodeText.indexOf("]"));
        return name + '<font color="#000">[' + nodeText + ']</font>';
    } catch (ex) {
        return nodeText;
    }
}

//产生节点的tip提示文字
function getNodeTitle(nodeText) {
    try {
        return nodeText;
    } catch (ex) {
        return nodeText;
    }
}

function filter(treeId, parentNode, childNodes) {
    if (!parentNode) {
        childNodes = [{
            name: "明唐测试账号",
            id: "27b22500-d22f-4adf-9a02-05414cd09d97",
            isParent: true
        }];
    }
    var p = '../img/';
    var parent;
    for (var i = 0, l = childNodes.length; i < l; i++) {
        parent = childNodes[i].isParent == "false" ? false : true;
        childNodes[i].title = childNodes[i].name;
        childNodes[i].name = getNodeName(childNodes[i].name);
        childNodes[i].isParent = parent;
        childNodes[i].pId = childNodes[i].pid;
        childNodes[i].icon = parent ? p + 'folder.png' : p + 'leaf.png';
    }
    return childNodes;
}

function onNodeCreated(event, treeId, treeNode) {
    if (!treeNode.pId && treeNode.level == 0) {
        tree.selectNode(treeNode);
        tree.reAsyncChildNodes(treeNode, "refresh");
    }
}

function onClick(event, treeId, treeNode, clickFlag) {
    //如果父页面有设置点击事件handler，则转至handler处理
    if (typeof parent.userTreeClickHandler == 'function') {
        parent.userTreeClickHandler(treeNode);
        return;
    }
    alert(treeNode.tId + ", " + treeNode.title);
}

/// 获取选中
function getSelectCustQueue() {
    var selected = tree.getSelectedNodes(),
        queue = [];
    if (!selected || !selected.length) {
        return [];
    }
    selected = selected[0];
    queue.push(selected.id);
    while (selected.getParentNode()) {
        selected = selected.getParentNode();
        queue.push(selected.id);
    }
    return queue.reverse();
}

function locateSubUser(queue) {
    if (!queue || queue.length == 0) {
        return;
    }
    tree.locateQueue = queue;
    var node = tree.getNodeByParam('id', tree.locateQueue.shift());
    if (node) {
        if (tree.locateQueue.length == 0) {
            tree.selectNode(node);
            tree.expandNode(node, true, false, true, false);

            onClick(null, null, node, null);
        } else {
            if (node.zAsync === true) {
                tree.selectNode(node);
                tree.expandNode(node, true, false, true, false);
                locateSubUser(tree.locateQueue);
            } else {
                tree.selectNode(node);
                tree.expandNode(node, true, false, true, false);
                tree.reAsyncChildNodes(node, "refresh");
            }
        }
    }
}

$(function () {
    $.fn.zTree.init($("#tree"), setting);
    tree = $.fn.zTree.getZTreeObj("tree");
    /*tree.setHeight = function (h) {
        $('#tree').height(h);
    };*/
});